
// 验证字符串中是否存在汉字
function isChina(val) {
  let reg = new RegExp('[\\u4E00-\\u9FFF]+','g')
  if (reg.test(val)) {
    // 含有中文字符
    return true
  } else {
    return false
  }
}

// 判断是否是图片
function  isImage(param) {
  let isArr = ['png', 'jpg', 'jpeg', 'pcx', 'psd', 'tiff', 'bmp', 'webp']
  function sliceStr(str) {
    const arr = str.split('.')
    const strs = arr[arr.length - 1]
    return isArr.includes(strs.toLocaleLowerCase())
  }
  if (Array.isArray(param)) {
    for (let item of param) {
      if (!sliceStr(item)) {
        return false
      } else {
        continue
      }
    }
    return true
  } else {
    return sliceStr(param)
  }
}



//判断输入的是不是手机号

function isPhone(phone){
  if (!phone || phone.length < 11) return false
  var myreg = /^[1][3,4,5,7,8,9][0-9]{9}$/;
  if (!myreg.test(phone)) {
    return false;
  } else {
    return true;
  }
}

function isName(name) {
  if (!name) return false
  let reg = /[0-9]/
  console.log(reg.test(name), '-- 65 --')
  if (reg.test(name)) {
    return false
  } else {
    return true
  }
}



//截取字符串最后一位
function sliceLastStr(str){

      var len = str.length
      str = str.slice(0,len-1)

      return str

}

//截取字符串
function sliceStr(str,num=1){

      var len = str.length - 5
      str = str.slice(len-num,len)

      return str

}


// 格式化时间
function  formatData(date) {
  if (!date) return false
  date = new Date(date);
  let Y = date.getFullYear()
  let M = date.getMonth() + 1
  let D = date.getDate()
  let h = date.getHours()
  let m = date.getMinutes()
  let s = date.getSeconds()
  function Time(time) {
    return time > 9 ? time : '0' + time
  }
  return `${Y}-${Time(M)}-${Time(D)} ${Time(h)}:${Time(m)}:${Time(s)}`
}

// 获取当前时间
function  formatNowDate(type=1) {
  let date = new Date();
  let Y = date.getFullYear()
  let M = date.getMonth() + 1
  let D = date.getDate()
  let h = date.getHours()
  let m = date.getMinutes()
  let s = date.getSeconds()
  function Time(time) {
    return time > 9 ? time : '0' + time
  }
  if(type==1){
	  return `${Y}-${Time(M)}-${Time(D)} ${Time(h)}:${Time(m)}:${Time(s)}`
  }else{
	  return `${Y}-${Time(M)}-${Time(D)}`
  }
}


//数组数据处理
function oldArray(val){
	return JSON.parse(JSON.stringify(val))
}

//获取序号
function returnNumber(page,index,pageSize=10){
   var page = page*1 - 1
   var index = index++
   var str
   if(index==pageSize){
      str = (page+1)*pageSize
   }else{
      str = page*pageSize + index
   }
   return str+1
}
//判断是不是JSON
function isJSON_test(str) {
        if (typeof str == 'string') {
            try {
                var obj=JSON.parse(str);
                return true;
            } catch(e) {
                return false;
            }
        }else{
			return false;
		}
        
 }
 
 function hasClass(ele, cls) {
   return !!ele.className.match(new RegExp('(\\s|^)' + cls + '(\\s|$)'))
 }
 /**
  * Add class to element
  * @param {HTMLElement} elm
  * @param {string} cls
  */
  function addClass(ele, cls) {
   if (!hasClass(ele, cls)) ele.className += ' ' + cls
 }
 
 /**
  * Remove class from element
  * @param {HTMLElement} elm
  * @param {string} cls
  */
  function removeClass(ele, cls) {
   if (hasClass(ele, cls)) {
     const reg = new RegExp('(\\s|^)' + cls + '(\\s|$)')
     ele.className = ele.className.replace(reg, ' ')
   }
 }
 
 function defaultImg(header_img){
 	if(!header_img){
 	    header_img = 'https://apishopsys.xucheng.top/uplode/timg.jpg'
		return header_img
 	}
 	// return 'https://sinacloud.net/header-img/' + header_img
	return '/imageHeader/url/' + header_img
 }　
 
 function generateRandomString(length) {
   let str = new Date().getTime() + Math.random().toString(36).substring(2, length + 2);
   return str;
 }
 
 function getRamNumber(len){
	 var result="";
	 for(var i=0;i<len;i++){
		 result+=Math.floor(Math.random()*9);
	 }
	 return result
 }
 
 //数组去重
 function unique(arr,key){
	 for(let i=0;i<arr.length;i++){
		 for(let j=i+1;j<arr.length;j++){
			 if(arr[i][key]==arr[j][key]){
				 arr.splice(j,1)
				 j--
			 }
		 }
	 }
	 return arr
 }
 
 // 处理时间开始
 function getGap(time, type){
	 var date = new Date(time).getDate()
	 var currentDate = new Date().getDate()
	 var Tm = getCurrentTime(new Date(time))
	 var currentTime = getCurrentTime(new Date())
	 if (!type) {
	 		if (currentTime[0] == Tm[0]) {
	 			if (currentTime[1] == Tm[1]) {
	 				if (currentTime[2] == Tm[2]) {
	           	return Tm[3] +':' + Tm[4]
	 				} else if (currentTime[2] - Tm[2] == 1) {
	 					return '昨天'
	 				} else if (currentTime[2] - Tm[2] < 7) {
	 					return week(new Date(time))
	 				} else {
	 					return Tm[1] + '-' + Tm[2]
	 				}
	 			} else {
	 				return Tm[1] + '-' + Tm[2]
	 			}
	 		} else {
	 			return Tm[0] + '-' + Tm[1] + '-' + Tm[2]
	 		}
	 	} else {
	 		if (currentTime[0] == Tm[0]) {
	 			if (currentTime[1] == Tm[1]) {
	 				if (currentTime[2] == Tm[2]) {
	 					return Tm[3] + ':' + Tm[4]
	 				} else if (currentTime[2] - Tm[2] == 1) {
	 					return '昨天' + Tm[3] + ':' + Tm[4]
	 				// } else if (currentTime[2] - Tm[2] < 7) {
	 				// 	return week(date)
	 				} else {
	 					return Tm[1] + '-' + Tm[2] + Tm[3] + ':' + Tm[4]
	 				}
	 			} else {
	 				return Tm[1] + '-' + Tm[2]
	 			}
	 		} else {
	 			return Tm[0] + '-' + Tm[1] + '-' + Tm[2]
	 		}
	 	}
	
 }
 function getCurrentTime(data) {
 	var Y = data.getFullYear(),
 			M = data.getMonth() + 1,
 			D = data.getDate(),
 			h = data.getHours(),
 			m = data.getMinutes(),
 			s = data.getSeconds();
 	function fromData(time) {
 		return time > 9 ? time : '0'+time
 	}
 	return [fromData(Y), fromData(M), fromData(D), fromData(h), fromData(m), fromData(s)]
 }
 function week (date) {
 	var weekArr = ["星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"]
 	var timeArr = getCurrentTime(date)
 	var hisDays = date.getDay()
 	var cDay = getDate().getDay()
 	var timeDif = cDay == 0 ? 7 - hisDays : cDay - hisDays
 	if (timeDif < 0 || hisDays == 0) {
 		return timeArr[1] + '-' + timeArr[2]
 	} else {
 		return weekArr[hisDays]
 	}
 }
 // 处理时间结束
 
 // 获取距离时间
 function getDistance(date,type=1)
 {
	 var time1 = new Date()
	 var time2 = new Date(date)
	 var diffTime = Math.round(Math.abs(time1.getTime() - time2.getTime()) / 1000)
	 var day = parseInt(diffTime/60/60/24);
	 var hour = parseInt(diffTime/60/60%24);
	 var min = parseInt(diffTime/60%60);
	 var sen = parseInt(diffTime%60);
	
	if(type==2){
		let bool = false
		
		// 转换为日期对象
		let dd = new Date();
		// 格式化日期为 "yyyy-MM-dd" 的字符串并存入数组
		let year = dd.getFullYear();
		let month = ("0" + (dd.getMonth() + 1)).slice(-2);
		let day = ("0" + dd.getDate()).slice(-2);
		let now = year + "-" + month + "-" + day
		let _update = date.split(' ')[0]
		if(now==_update){
					 bool = true
		}
		return bool
	}else{
		if(day>0){
				 return day+'天'
		}else if(hour>0){
				 return hour+'小时'
		}else{
				 return min+'分钟'
		}
	}
	
 }
 
 // 过滤中文
function removeChinese(str)
{
	return str.replace(/[\u4e00-\u9fa5]/g, '');
}
 
export default {
  removeChinese,
  getDistance,
  getGap,
  sliceStr,	
  unique,
  getRamNumber,
  formatNowDate,
  generateRandomString,	
  defaultImg,	
  isJSON_test,
  returnNumber,
  oldArray,
  isChina,
  isImage,
  isPhone,
  isName,
  sliceLastStr,
  formatData,
  addClass,
  removeClass
}
